package com.shangye.shop.service.impl;

import com.shangye.shop.dao.ProductDao;
import com.shangye.shop.dao.ProductImgDao;
import com.shangye.shop.dto.ImageHolder;
import com.shangye.shop.dto.ProductExecution;
import com.shangye.shop.entity.Product;
import com.shangye.shop.entity.ProductImg;
import com.shangye.shop.enums.ProductStateEnum;
import com.shangye.shop.exceptions.ProductOperationException;
import com.shangye.shop.service.ProductService;
import com.shangye.shop.util.ImageUtil;
import com.shangye.shop.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProductImgDao productImgDao;

    @Override
    @Transactional
    public ProductExecution addProduct(Product product, ImageHolder thumbnail, List<ImageHolder> productImgList) throws ProductOperationException {
        if(product != null &&product.getShop() != null && product.getShop().getId() != null){
            product.setCreateTime(new Date());
            product.setUpdateTime(new Date());
            product.setEnableStatus(1);
            if(thumbnail != null){
                addThumbnail(product, thumbnail);
            }
            try{
                int effectedNum = productDao.insertProduct(product);
                if(effectedNum <= 0){
                    throw new ProductOperationException("创建商品失败");
                }
            }catch (Exception e){
                e.printStackTrace();
                throw new ProductOperationException("创建商品失败：" + e.toString());
            }

            if(productImgList != null && productImgList.size() > 0){
                addProductImgList(product, productImgList);
            }
            return new ProductExecution(ProductStateEnum.SUCCESS, product);
        }

        return new ProductExecution(ProductStateEnum.EMPTY);
    }

    public void addThumbnail(Product product, ImageHolder thumbnail){
        String dest = PathUtil.getShopImagePath(product.getShop().getId());
        String thumbnailAddress = ImageUtil.generateThumbnail(thumbnail, dest);
        product.setImgAddress(thumbnailAddress);
    }

    public void addProductImgList(Product product, List<ImageHolder> productImgHolderList) {
        String dest = PathUtil.getShopImagePath(product.getShop().getId());
        List<ProductImg> productImgList = new ArrayList<>();
        for(ImageHolder productImgHolder: productImgHolderList){
            String imgAddress = ImageUtil.generateNormalImg(productImgHolder, dest);
            ProductImg productImg = new ProductImg();
            productImg.setUrl(imgAddress);
            productImg.setProductId(product.getId());
            productImg.setCreateTime(new Date());
            productImg.setPriority(2);
            productImg.setDesc("---");
            productImgList.add(productImg);
        }
        if(productImgList.size() > 0){
            try{
                int effectedNum = productImgDao.batchInsertProductImg(productImgList);
                if(effectedNum <= 0){
                    throw new ProductOperationException("创建商品详图失败");
                }
            }catch (Exception e){
                throw new ProductOperationException("创建商品详图失败：" + e.toString());
            }
        }
    }
}
